﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ORMAC.Core
{
    /// <summary>
    /// class to contain a collection of context objects
    /// </summary>
    public class ContextMemberCollection : Dictionary<Type, HashSet<string>>
    {
        private Dictionary<Type, HashSet<string>> contextCache=new Dictionary<Type,HashSet<string>>();
        /// <summary>
        /// checks if a member is in context or not
        /// </summary>
        /// <param name="type"></param>
        /// <param name="member"></param>
        /// <returns></returns>
        internal bool IsInContext(Type type, string member,Type memberType)
        {
            //return true if this is a value type
            if (!memberType.IsClass || memberType==typeof(string))
                return true;
            //by default if a type is not in the dictionary, all its members are assumed to be in the context
            return !this.ContainsKey(type) || this[type].Contains(member);
        }
        /// <summary>
        /// checks if a member is in context or not
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="member"></param>
        /// <returns></returns>
        internal bool IsInContext<T>(string member,Type memberType)
        {
            return IsInContext(typeof(T), member,memberType);
        }
        internal void Add2ContextCache(Type type, string member)
        {
            lock (this)
            {
                if (!contextCache.ContainsKey(type))
                {
                    contextCache.Add(type, new HashSet<string>());
                }
                contextCache[type].Add(member);
            }
        }
        internal bool IsInContextCache(Type type, string member, Type memberType)
        {
            return contextCache!=null && contextCache.ContainsKey(type) && contextCache[type].Contains(member);
        }

    }
}
